package org.kfive.nova.algorithm;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.base.Verify;
import com.google.common.collect.Lists;

import java.util.List;

/*
* BruteForce.java
* Created  by  lizhanwu
* Created on 2016-5-25 10:47
*/
public class BruteForce {

    /**
     * 字符匹配算法-BF算法
     * @return
     */
    public static List<Integer> bruteforce(String source,String pattern){
        List<Integer> result = Lists.newArrayList();
        Preconditions.checkArgument(!Strings.isNullOrEmpty(source),"参数错误");
        Preconditions.checkArgument(!Strings.isNullOrEmpty(pattern),"参数错误");
        int i=0,j = 0;
        while (i<(source.length()-pattern.length())){
            if(source.charAt(i)==pattern.charAt(j)){
                i++;
                j++;
            }else {
                i = i-j+1;
                j=0;
            }
            if(j==pattern.length()){
                result.add(i-j);
                i++;
                j=0;
            }
        }
        return result;
    }

}
